package com.http;

import java.util.ArrayList;

import org.json.JSONArray;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.GiftStruct;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:UserListTask
 * @Description:TODO获取用户列表
 * @author zhuw
 * @date 2013-8-4 下午5:20:45
 */
public class GiftListTask extends AsyncTask<String, Void, ResultGiftList> {
	public static interface CallBack extends NetWorkCallBack {
		/**
		 * 
		 * @Description:TODO处理底层获取后的数据
		 * @param result
		 * @return void
		 * @author zhuw
		 * @date 2013-4-24 上午10:19:30
		 */
		public abstract void procGiftListData(ArrayList<GiftStruct> dataList);

	}

	CallBack callback;

	public GiftListTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultGiftList doInBackground(String... params) {
		ResultGiftList data = null;
		try {
			ResEntity re = null;
			String type=params[0];
			if (type.equalsIgnoreCase(GiftStruct.TYPE_RECEIVE)) {
				re = NetConUtil.sendGetRequestEntity(ServerPath
						.getReceivedGiftPath(params[1]));
			} else if (type.equalsIgnoreCase(GiftStruct.TYPE_SEND)) {
				re = NetConUtil.sendGetRequestEntity(ServerPath
						.getSendGiftPath(params[1]));
			}
			if (re.code == ResEntity.S_OK) {
				data = new ResultGiftList(CommonData.H_FLAG_SUCCESS, null,
						JsonUtil.parseGiftList(new JSONArray(re.jsonStr),
								params[0]));
			} else if (re.code == ResEntity.S_FAILED) {

				data = new ResultGiftList(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + "    " + re.jsonStr, null);

			} else {
				data = new ResultGiftList(CommonData.H_FLAG_ERROR, "code:"
						+ re.code + ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data = new ResultGiftList(CommonData.H_FLAG_ERROR,
					ErrorCode.ERROR_MSG, null);
			e.printStackTrace();

		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultGiftList result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procGiftListData(result.dataList);

		} else {
			callback.procError(result.msg);
		}

	}
}
